1   /*
2    * Copyright (C) 2009 The Guava Authors
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package com.google.common.collect.testing;
18  
19  import static com.google.common.collect.testing.Helpers.orderEntriesByKey;
20  
21  import com.google.common.annotations.GwtCompatible;
22  
23  import java.util.List;
24  import java.util.Map;
25  import java.util.Map.Entry;
26  
27  /**
28   * Implementation helper for {@link TestMapGenerator} for use with enum maps.
29   *
30   * @author Kevin Bourrillion
31   */
32  @GwtCompatible
33  public abstract class TestEnumMapGenerator
34      implements TestMapGenerator<AnEnum, String> {
35  
36    @Override
37    public SampleElements<Entry<AnEnum, String>> samples() {
38      return new SampleElements<Entry<AnEnum, String>>(
39          Helpers.mapEntry(AnEnum.A, "January"),
40          Helpers.mapEntry(AnEnum.B, "February"),
41          Helpers.mapEntry(AnEnum.C, "March"),
42          Helpers.mapEntry(AnEnum.D, "April"),
43          Helpers.mapEntry(AnEnum.E, "May")
44      );
45    }
46  
47    @Override
48    public final Map<AnEnum, String> create(Object... entries) {
49      @SuppressWarnings("unchecked")
50      Entry<AnEnum, String>[] array = new Entry[entries.length];
51      int i = 0;
52      for (Object o : entries) {
53        @SuppressWarnings("unchecked")
54        Entry<AnEnum, String> e = (Entry<AnEnum, String>) o;
55        array[i++] = e;
56      }
57      return create(array);
58    }
59  
60    protected abstract Map<AnEnum, String> create(
61        Entry<AnEnum, String>[] entries);
62  
63    @Override
64    @SuppressWarnings("unchecked")
65    public final Entry<AnEnum, String>[] createArray(int length) {
66      return new Entry[length];
67    }
68  
69    @Override
70    public final AnEnum[] createKeyArray(int length) {
71      return new AnEnum[length];
72    }
73  
74    @Override
75    public final String[] createValueArray(int length) {
76      return new String[length];
77    }
78  
79    /** Returns the elements sorted in natural order. */
80    @Override
81    public Iterable<Entry<AnEnum, String>> order(
82        List<Entry<AnEnum, String>> insertionOrder) {
83      return orderEntriesByKey(insertionOrder);
84    }
85  }